//
//  VideoCell.swift
//  Youtube2
//
//  Created by 劢克科技 on 2019/5/5.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

class VideoCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        thumbImage.image = nil
        avatar.image = nil
    }
    
    //MARK: - Setter
    
    var video: VideosModel? {
        didSet {
            videoTitle.text = self.video?.title
            videoDescription.text = self.video?.desc
            setupThumbnail()
            setupAvatar()
        }
    }
    
    fileprivate func setupAvatar() {
        avatar.downloadImage(urlString: video!.avatar)
    }
    
    fileprivate func setupThumbnail() {
        thumbImage.downloadImage(urlString: video!.thumb)
    }
    
    //MARK: - Getter
    
    lazy var thumbImage: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        image.clipsToBounds = true
        image.translatesAutoresizingMaskIntoConstraints = false
        return image
    }()
    
    lazy var separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    lazy var avatar: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        image.clipsToBounds = true
        image.layer.cornerRadius = 20
        image.translatesAutoresizingMaskIntoConstraints = false
        return image
    }()
    
    lazy var videoTitle: UILabel = {
        let label = UILabel()
        label.text = "Let's build that app!"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    lazy var videoDescription: UILabel = {
        let label = UILabel()
        label.text = "Let's build that app!Let's build that app!Let's build that app!Let's build that app!Let's build that app!"
        label.numberOfLines = 2
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.lightGray
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    fileprivate func setupViews() {
        contentView.addSubview(thumbImage)
        contentView.addSubview(separatorView)
        contentView.addSubview(avatar)
        contentView.addSubview(videoTitle)
        contentView.addSubview(videoDescription)
        
        thumbImage.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
        thumbImage.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10).isActive = true
        thumbImage.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
        let height = (frame.width - 10 - 10) * 9 / 16
        thumbImage.heightAnchor.constraint(equalToConstant: height).isActive = true
        
        avatar.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
        avatar.topAnchor.constraint(equalTo: thumbImage.bottomAnchor, constant: 10).isActive = true
        avatar.widthAnchor.constraint(equalToConstant: 40).isActive = true
        avatar.heightAnchor.constraint(equalToConstant: 40).isActive = true
        
        videoTitle.leftAnchor.constraint(equalTo: avatar.rightAnchor, constant: 10).isActive = true
        videoTitle.topAnchor.constraint(equalTo: avatar.topAnchor).isActive = true
        videoTitle.rightAnchor.constraint(equalTo: thumbImage.rightAnchor).isActive = true
        
        videoDescription.leftAnchor.constraint(equalTo: avatar.rightAnchor, constant: 10).isActive = true
        videoDescription.topAnchor.constraint(equalTo: videoTitle.bottomAnchor, constant: 2).isActive = true
        videoDescription.rightAnchor.constraint(equalTo: thumbImage.rightAnchor).isActive = true
        
        separatorView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        separatorView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        separatorView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        separatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
    }
}
